// SPDX-FileCopyrightText: 2025 UnionTech Software Technology Co., Ltd.
// SPDX-License-Identifier: MIT

package main

import (
	"time_exporter/internal/server"
	"time_exporter/pkg/utils"

	"github.com/sirupsen/logrus"
)

func Run(name string, version string) error {
	s := server.NewServer(name, version)

	s.PrintVersion()
	err := s.SetUp()
	if err != nil {
		logrus.Errorf("SetUp error: %v", err)
		return err
	}
	go func() {
		err := s.Run()
		if err != nil {
			logrus.Errorf("Run error: %v", err)
			s.Error = err
		}

		s.Exit()
	}()

	// 启动信号处理
	go utils.HandleSignals(func() {
		logrus.Info("Received shutdown signal, stopping server...")
		s.Stop()
	})

	<-s.ExitSignal
	s.Stop()
	logrus.Info("Exit exporter server completed")
	return s.Error
}
